<?php
ptaste();
require_once(dirname(__FILE__)."/mailer.php");
require_once(dirname(__FILE__)."/dbimages.php");
require_once(dirname(__FILE__)."/banners.php");
class JEngUtilHelper
{
  private $aUtils=null;
  function __construct($utils)
  {
    $this->aUtils=$utils;
  }
  
  function utils()
  {
    return $this->aUtils;
  }
}

class JEngUtilHTMLHelper extends JEngUtilHelper
{
}

class JEngUtilJSHelper extends JEngUtilHelper
{
}

class JEngUtilGWTHelper extends JEngUtilHelper
{
  function addgwt($gwt)
  {
    $this->addOgwt($this->utils()->site()->conf()->get('site_host'),$gwt);
  }
  
  function addOgwt($site,$gwt)
  {
	$xgwt=$this->utils()->site()->emptyXML("mgwt");
	if ($gwt!=null)
	{
	  $scr=$xgwt->createElement("script");
	  $attr=$xgwt->createAttribute("type");
	  $attr->appendChild($xgwt->createTextNode("text/javascript"));
	  $scr->appendChild($attr);
	  $attr=$xgwt->createAttribute("src");
	  $attr->appendChild($xgwt->createTextNode($site."/scripts/gwt/".$gwt."/".$gwt.".nocache.js"));
	  $scr->appendChild($attr);
	  $scr->appendChild($xgwt->createTextNode(""));
	  $xgwt->documentElement->appendChild($scr);
	  $scr=$xgwt->createElement("gwt");
	  $xgwt->documentElement->appendChild($scr);
	}
	JEngXCombiner::s_render($this->utils()->site()->doc,$xgwt,"gwt","mgwt",
		JEngXCombiner::HOW_ONCE | JEngXCombiner::HOW_REPLACE);
  }
}

class JEngUtilStringHelper extends JEngUtilHelper
{
}

class JEngUtilDBHelper extends JEngUtilHelper
{
}

class JEngUtilCommonHelper extends JEngUtilHelper
{
	function guid()
	{
	return sprintf('%04x%04x%04x%03x4%04x%04x%04x%04x',
       mt_rand(0, 65535), mt_rand(0, 65535), // 32 bits for "time_low"
       mt_rand(0, 65535), // 16 bits for "time_mid"
       mt_rand(0, 4095),  // 12 bits before the 0100 of (version) 4 for "time_hi_and_version"
       bindec(substr_replace(sprintf('%016b', mt_rand(0, 65535)), '01', 6, 2)),
           // 8 bits, the last two of which (positions 6 and 7) are 01, for "clk_seq_hi_res"
           // (hence, the 2nd hex digit after the 3rd hyphen can only be 1, 5, 9 or d)
           // 8 bits for "clk_seq_low"
       mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535) // 48 bits for "node" 
	   );
	}
	
	function varUnhack($value)
	{
	  $res=htmlspecialchars($value);
	  $res=str_replace(";","%3C",$res);
	  return res;
	}
	
	function gpVar($varname)
	{
	  $res="";
	  if (isset($_GET[$varname]))
	    $res=$_GET[$varname];
	  if (isset($_POST[$varname]))
	    $res=$_POST[$varname];
	  return $res;
	}

	function gpuVar($varname)
	{
		return $this->varUnhack($this->gpVar($varname));
	}
}

class JEngUtilXMLHelper extends JEngUtilHelper
{
   function emptyXML($rootname="result")
   {
     $doc=new DOMDocument("1.0","utf-8");
	 $doc->encoding="utf-8";
	 $root=$doc->createElement($rootname);
	 $doc->appendChild($root);
	 return $doc;
   }
  
   function nodeDoc($node)
   {
     return $node->ownerDocument;
   }
   
   function appendNode($parent,$name)
   {
     $nd=$this->nodeDoc($parent)->createElement($name);
	 $parent->appendChild($nd);
	 return $nd;
   }
   
   function appendTextNode($parent,$name,$text)
   {
	$nd=$this->appendNode($parent,$name);
	$nd->appendChild($this->nodeDoc($parent)->createTextNode($text));
	return $nd;
   }
   
   function appendAttribute($node,$name,$value)
   {
     $doc=$this->nodeDoc($node);
     $nd=$doc->createAttribute($name);
	 $nd->appendChild($doc->createTextNode($value));
	 $node->appendChild($nd);
	 return $nd;
   }
}


class JEngUtils extends SiteObject
{
  private $aHTMLHelper=null;
  private $aJSHelper=null;
  private $aGWTHelper=null;
  private $aStringHelper=null;
  private $aDBHelper=null;
  private $aCommonHelper=null;
  private $aXMLHelper=null;
  private $aMailer=null;
  private $aDBImage=null;
  private $aBanners=null;

  function __construct($site,$logger=null)
  {
    parent::__construct($site,$logger);
  }
  
  function htmlHelper()
  {
    if (!$this->aHTMLHelper) $this->aHTMLHelper=new JEngUtilHTMLHelper($this);
	return $this->aHTMLHelper;
  }
  
  function jsHelper()
  {
    if (!$this->aJSHelper) $this->aJSHelper=new JEngUtilJSHelper($this);
    return $this->aJSHelper;
  }
  
  function gwtHelper()
  {
    if (!$this->aGWTHelper) $this->aGWTHelper=new JEngUtilGWTHelper($this);
    return $this->aGWTHelper;
  }
  
  function stringHelper()
  {
    if (!$this->aStringHelper) $this->aStringHelper=new JEngUtilStringHelper($this);
    return $this->aStringHelper;
  }
  
  function dbHelper()
  {
    if (!$this->aDBHelper) $this->aDBHelper=new JEngUtilDBHelper(this);
    return $this->aDBHelper;
  }
  
  function commonHelper()
  {
    if (!$this->aCommonHelper) $this->aCommonHelper=new JEngUtilCommonHelper($this);
    return $this->aCommonHelper;
  }
  
  function xmlHelper()
  {
    if (!$this->aXMLHelper) $this->aXMLHelper=new JEngUtilXMLHelper($this);
    return $this->aXMLHelper;
  }
  
  function mailer()
  {
    if (!$this->aMailer) $this->aMailer=new JEngMailer($this);
	return $this->aMailer;
  }

  function dbImage()
  {
    if (!$this->aDBImage) $this->aDBImage=new JEngDBImage($this);
	return $this->aDBImage;
  }
  
  function bannerProc()
  {
    if (!$this->aBanners) $this->aBanners=new JEngBannerProcessor($this);
	return $this->aBanners;
  }
}

?>